package com.ah.suams.util;


import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class VCodeUtil {

    //设置图片属性
    private static void setField(Graphics2D graphics,int width,int height){
        //设置验证码背景色
        graphics.setColor(Color.gray);
        //填充背景
        graphics.fillRect(0,0, width, height);
        //设置字体
        graphics.setFont(new Font("微软雅黑",Font.BOLD,20));
    }

    //图片绘画
    public static String drawImage(BufferedImage image,int width,int height){
        Graphics2D graphics = (Graphics2D) image.getGraphics();

        //设置图片属性
        setField(graphics,width,height);

        StringBuilder vcodeText = new StringBuilder();
        String text = "";
        int x = 5;
        int y = 28;

        for(int i = 0;i < 4;i++){
            text = RandomUtil.getRandomStr();
            vcodeText.append(text);

            graphics.setColor(getRandomColor());
            graphics.drawString(text,x,y);

            x += 18;
        }

        return vcodeText.toString();
    }

    //随机颜色
    private static Color getRandomColor() {
        Random ran = new Random();
        Color color = new Color(ran.nextInt(256),
                ran.nextInt(256), ran.nextInt(256));
        return color;
    }

}
